package cn.itcast.itcaststore.serive.impl;

import cn.itcast.itcaststore.dao.NoticeDao;
import cn.itcast.itcaststore.dao.impl.NoticeDaoImpl;
import cn.itcast.itcaststore.domain.Notice;
import cn.itcast.itcaststore.serive.NoticeService;
import cn.itcast.itcaststore.utils.DataSourceUtils;

import java.sql.SQLException;
import java.util.List;

/**
 * @Auther: 万秋泊
 * @Date: 2021/10/27 10:45
 * @Description:
 */
public class NoticeServiceImpl implements NoticeService {
    private NoticeDao noticeDao = new NoticeDaoImpl();

    @Override
    public Notice getRecentNotice() {
        return noticeDao.getRecentNotice();
    }


    @Override
    public List<Notice> findAllNotice()  {
        try {
            return noticeDao.findAllNotice();
        } catch (SQLException e) {
            throw new RuntimeException("查询所有的公告失败！");
        }
    }

    @Override
    public int addNotice(Notice notice) throws SQLException {
        return noticeDao.addNotice(notice);
    }

    @Override
    public void deleteNotice(int n_id) throws SQLException {
        try {
            DataSourceUtils.startTransaction();;//开启事务
            noticeDao.deleteNotice(n_id);
        }catch (SQLException e){
            e.printStackTrace();
            try {
                DataSourceUtils.rollback();
            }catch (SQLException e1){
                e1.printStackTrace();
            }
        }finally {
            try {
                DataSourceUtils.releaseAndCloseConnection();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public void updateNotice(Notice notice) throws SQLException {

        try {
            DataSourceUtils.startTransaction();;//开启事务
           noticeDao.updateNotice(notice);
        }catch (SQLException e){
            e.printStackTrace();
            try {
                DataSourceUtils.rollback();
            }catch (SQLException e1){
                e1.printStackTrace();
            }
        }finally {
            try {
                DataSourceUtils.releaseAndCloseConnection();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }

    }

    @Override
    public Notice findNoticeById(int n_id) throws SQLException {
        return noticeDao.findNoticeById(n_id);
    }



}
